package com.nanyang.tms.manager.waybill.shipment.handler;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.handler.AbstractTransHandler;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.receipt.ReceiptRelationDao;
import com.nanyang.tms.dao.shipment.ShipmentBillDao;
import com.nanyang.tms.dao.shipment.ShipmentRelationDao;
import com.nanyang.tms.domain.receipt.ReceiptRelationVO;
import com.nanyang.tms.domain.shipment.ShipmentBillDO;
import com.nanyang.tms.domain.shipment.ShipmentRelationDO;
import com.nanyang.tms.domain.waybill.context.UpdateForecastContext;
import com.nanyang.tms.domain.waybill.shipment.context.CalculateSupplierWeightContext;
import com.nanyang.tms.domain.waybill.shipment.context.SaveWaybillShipmentContext;

@Component
public class UpdateShipmentHandler extends AbstractTransHandler{
    @Autowired
	private ShipmentBillDao  shipmentBillDao;
    @Autowired
    private ShipmentRelationDao shipmentRelationDao;
    @Autowired
    private ReceiptRelationDao receiptRelationDao;
	@Override
	public boolean isAsync() {
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
		SaveWaybillShipmentContext saveWaybillShipmentContext=	(SaveWaybillShipmentContext) context.getInnerContext(SaveWaybillShipmentContext.class);
		CalculateSupplierWeightContext calculateSupplierWeightContext=	(CalculateSupplierWeightContext) context.getInnerContext(CalculateSupplierWeightContext.class);
		try {
			shipmentBillDao.updateShipmentAccrual(buildShipmentBill(saveWaybillShipmentContext, calculateSupplierWeightContext));
			ReceiptRelationVO receiptRelationVO = receiptRelationDao.selectByWaybillCode(saveWaybillShipmentContext.getWaybillCode());
			shipmentRelationDao.insert(bulidShipmentRelation(saveWaybillShipmentContext, receiptRelationVO));
			
			
			
			UpdateForecastContext updateForecastContext = (UpdateForecastContext) context
					.getInnerContext(UpdateForecastContext.class); 
			updateForecastContext.setForecastCode(receiptRelationVO.getForecastCode());
			updateForecastContext.setState(2);
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		return true;
	}
	
	private ShipmentBillDO buildShipmentBill(SaveWaybillShipmentContext saveWaybillShipmentContext,CalculateSupplierWeightContext calculateSupplierWeightContext){
		ShipmentBillDO build = ShipmentBillDO.builder()
		.shipmentCode(saveWaybillShipmentContext.getShipmentCode())
		.totalNum(saveWaybillShipmentContext.getTotalNum())
		.totalChargeWeight(calculateSupplierWeightContext.getTotalChargeWeight())
		.totalWeight(calculateSupplierWeightContext.getTotalWeight())
		.totalVolumeWeight(calculateSupplierWeightContext.getTotalVolumeWeight())
		.shipmentTime(new Date())
		.build();
		build.setGmtModifiedPin(saveWaybillShipmentContext.getPin());
		build.setGmtModified(new Date());
		return build;
	}
	private ShipmentRelationDO bulidShipmentRelation(SaveWaybillShipmentContext saveWaybillShipmentContext,ReceiptRelationVO receiptRelationVO){
		ShipmentRelationDO build = ShipmentRelationDO.builder()
		.shipmentCode(saveWaybillShipmentContext.getShipmentCode())
		.forecastCode(receiptRelationVO.getForecastCode())
		.supplierNo(receiptRelationVO.getSupplierNo())
		.saleProductCode(receiptRelationVO.getSaleProductCode())
		.saleProductName(receiptRelationVO.getSaleProductName())
		.customerNo(receiptRelationVO.getCustomerNo())
		.waybillCode(saveWaybillShipmentContext.getWaybillCode())
		.build();
		build.setIp(saveWaybillShipmentContext.getIp());
		build.setGmtCreatePin(saveWaybillShipmentContext.getPin());
		build.setGmtModifiedPin(saveWaybillShipmentContext.getPin());
		return build;
		
	}

}
